package com.hyfrogx.modules.manage.service.course.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.exception.ServiceException;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.modules.manage.dao.course.CourseTypeDao;
import com.hyfrogx.modules.manage.entity.course.CourseTypeEntity;
import com.hyfrogx.modules.manage.service.course.CourseTypeService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author 罗春杰-技术部
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeDao, CourseTypeEntity> implements CourseTypeService {
    @Override
    public void updateSort(List<CourseTypeEntity> list) throws ServiceException {
        this.baseMapper.updateSort(list);
    }

    @Override
    public void removeType(Integer id) throws MyException {
        CourseTypeEntity courseTypeEntity = this.baseMapper.selectById(id);
        courseTypeEntity.setDeleted(2);
        int i = this.baseMapper.updateById(courseTypeEntity);
        if(i!=1){
            throw new MyException("删除失败");
        }
    }

    @Override
    public Page selectTypePage(QueryExt queryExt) {
        Map map = new HashMap();
        map.putAll(queryExt.getCondition());
        queryExt.setRecords(this.baseMapper.selectPage(queryExt,map));
        return queryExt;
    }

    @Override
    public Boolean addCourseType(CourseTypeEntity courseTypeEntity) {
        Integer maxSort = this.baseMapper.findMaxSort();
        if(maxSort == null ){
            maxSort=0;
            courseTypeEntity.setSort(maxSort);
        }
        courseTypeEntity.setSort(maxSort+1);
        this.baseMapper.insert(courseTypeEntity);
        return true;
    }

    @Override
    public Page ApiListGetByPage(QueryExt queryExt) {
        Map map = new HashMap();
        map.putAll(queryExt.getCondition());
        queryExt.setRecords(this.baseMapper.ApiListGetByPage(queryExt,map));
        return queryExt;
    }
}
